//
//  WaterfallLayout.m
//  练习
//
//  Created by hcios on 2017/7/7.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "WaterfallLayout.h"

@interface WaterfallLayout()

@property (nonatomic,assign) NSUInteger numberOfItems;

@property (nonatomic,strong) NSMutableArray *columnHeights;

@property (nonatomic,strong) NSMutableArray *itemAttributes;

-(NSInteger)indexForLongestColumn;
            
-(NSInteger)indexForShortestColumn;

@end

@implementation WaterfallLayout

-(NSMutableArray *)columnHeights{
    if (nil == _columnHeights) {
        self.columnHeights = [NSMutableArray array];
    }
    return _columnHeights;
}


-(NSMutableArray *)
itemAttributes{
    if (nil == _itemAttributes) {
        self.itemAttributes = [NSMutableArray array];
    }
    return _itemAttributes;
}

-(NSInteger)indexForLongestColumn{
    NSInteger longestIndex = 0;
    
    CGFloat longestHeight = 0;
    
    for (int i = 0; i < self.numberOfColumn; i++) {
        CGFloat currentHeight = [self.columnHeights[i] floatValue];
        
        if (currentHeight > longestHeight) {
            longestHeight = currentHeight;
            longestIndex = i;
        }
    }
    return longestIndex;
}



-(NSInteger)indexForShortestColumn{
    NSInteger shortestIndex = 0;
    
    CGFloat shortestHeight = MAXFLOAT;
    for (int i = 0; i < self.numberOfColumn; i++) {
        CGFloat currentHeight = [self.columnHeights[i] floatValue];
        if (currentHeight < shortestHeight) {
            
            shortestHeight = currentHeight;
            shortestIndex = i;
        }
    }
    return shortestIndex;
}


-(void)prepareLayout{
    [super prepareLayout];
    [self.itemAttributes removeAllObjects];
    
    for (int i = 0; i < self.numberOfColumn; i++) {
        self.columnHeights[i] = @(self.sectionInsets.top);
    }
    
    self.numberOfItems = [self.collectionView numberOfItemsInSection:0];
    
    for (int i = 0; i < self.numberOfItems; i++) {
        NSInteger shortIndex = [self indexForShortestColumn];
        
        CGFloat shortestH = [self.columnHeights[shortIndex] floatValue];
        
        CGFloat detalX = self.sectionInsets.left + (self.itemSize.width + self.insertItemSpacing) * shortIndex;
        
        CGFloat detalY = shortestH + self.insertItemSpacing;
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        
        CGFloat itemHeight = 0;
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(heightForItemIndexPath:)]) {
            itemHeight = [self.delegate heightForItemIndexPath:indexPath];
        }
        
        
        UICollectionViewLayoutAttributes *la = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        la.frame = CGRectMake(detalX, detalY, self.itemSize.width, itemHeight);
        
        [self.itemAttributes addObject:la];
        
        self.columnHeights[shortIndex] = @(detalY + itemHeight);
    }
}


-(CGSize)collectionViewContentSize{
    
    NSInteger longestIndex = [self indexForLongestColumn];
    CGFloat longestH = [self.columnHeights[longestIndex] floatValue];
    
    CGSize contentSize = self.collectionView.frame.size;
    contentSize.height = longestH + self.sectionInsets.bottom;
    
    return contentSize;
}


-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    return self.itemAttributes;
}

@end
